var app = getApp();
Page({
  data: {
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    session_key:''
  },

  authorization: function (e,sessionKey) {
    // 查看是否授权
    wx.getSetting({
      success(res) {
        if (res.authSetting['scope.userInfo']) {
          // 已经授权，可以直接调用 getUserInfo 获取头像昵称
          wx.getUserInfo({
            success(res) {
              /*解密会员信息*/
              var that = this;
              wx.request({
                url: app.globalData.url + "/applet/decode",
                data: {
                  sessionKey: sessionKey,
                  encryptedData: res.encryptedData,
                  ivStr: res.iv,
                  // parentId: wx.getStorageSync('parentId')
                },
                success: function (res) {
                  // wx.setStorage({//unionid存入本地缓存
                  //   key: "memberInfo",
                  //   data: res.data.data
                  // });

                  console.log(res.data);
                  wx.setStorage({
                    key: 'userInfo',
                    data: res.data.apiResult
                  });
                  wx.setStorage({
                    key: 'memberInfo',
                    data: res.data.member
                  });
                  wx.navigateBack({
                    delta: 1
                  })
                }
              })

            }
          })
        } else {
          console.log("未授权，需要从新授权");

        }

      }
    })

  },
  onLoad() {

  },
  bindGetUserInfo(e) {
    console.log(e);
    // 登录
    wx.login({
      success: res => {
        // 发送 res.code 到后台换取 openId, sessionKey, unionId
        var that = this;
        wx.request({
          url: app.globalData.url + "/applet/getSessionKey",
          data: {
            code: res.code,
            // parentId: wx.getStorageSync('parentId')
          },
          success: function (res) {

            console.log(e.detail.userInfo)
            
            that.setData({
              session_key: res.data.session_key
            });
            that.authorization(e,res.data.session_key);

          }
        })
      }
    })


    

  }
})